//
//  ViewController.m
//  10-掌握-mapkit框架-基本使用
//
//  Created by J on 16/2/28.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "AnnoTation.h"
@interface ViewController ()<MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapVIew;
@property (strong, nonatomic)  CLLocationManager *manager;
@property(nonatomic,strong)CLGeocoder* geoCoder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [CLLocationManager new];
    [self.manager requestAlwaysAuthorization];
    self.geoCoder = [CLGeocoder new];
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取当前触摸点
    CGPoint poin = [[touches anyObject] locationInView:self.mapVIew];
   CLLocationCoordinate2D pt = [self.mapVIew convertPoint:poin toCoordinateFromView:self.mapVIew];
    [self addAnnoWithPoint:pt];
  
 }



- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //移除大头针
    NSArray* annos = self.mapVIew.annotations;
    [self.mapVIew removeAnnotations:annos];
    
}



- (void)addAnnoWithPoint:(CLLocationCoordinate2D)point{
    
   __block AnnoTation* anno = [AnnoTation new];
    anno.coordinate = point;
    anno.title = @"小码哥";
    anno.subtitle = @"大神一班";
    [self.mapVIew addAnnotation:anno];
    CLLocation* loc = [[CLLocation alloc]initWithLatitude:anno.coordinate.latitude longitude:anno.coordinate.longitude];
    [self.geoCoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (!error) {
//            NSLog(@"%@",placemarks.firstObject.name);
            anno.title = placemarks.firstObject.locality;
            anno.subtitle = placemarks.firstObject.thoroughfare;
        }
        
    }];
}




///  当我们添加大头针时候
///
///  @param mapView    地图
///  @param annotation 打头针
///
///  @return 大头针视图
//
//- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
//    MKAnnotationView* anno = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"];
//    anno.frame = CGRectMake(0, 0, 100, 100);
//    anno.backgroundColor = [UIColor orangeColor];
//    return  anno;
//    
//}


//更新到位置
//MKUserLocation位置对象
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    

    
    
}



@end
